package com.qky.note.part5ShortToUnicode; /***
 * Excerpted from "The Definitive ANTLR 4 Reference",
 * published by The Pragmatic Bookshelf.
 * Copyrights apply to this code. It may not be used to create training material, 
 * courses, books, articles, and the like. Contact us if you are in doubt.
 * We make no guarantees that this code is fit for any purpose. 
 * Visit http://www.pragmaticprogrammer.com/titles/tpantlr2 for more book information.
***/
// import ANTLR's runtime libraries

import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.ParseTreeWalker;

public class Translate {
    public static void main(String[] args) {
        // 词法分析、语法分析并得到语法树(的根节点上下文对象)
        ArrayInitLexer lexer = new ArrayInitLexer(CharStreams.fromString("{1,2,3,4}"));
        ArrayInitParser parser = new ArrayInitParser(new CommonTokenStream(lexer));
        ArrayInitParser.InitContext tree = parser.init();
        System.out.println(tree.toStringTree(parser));
        // 创建一个能触发回调函数的语法分析树遍历器
        ParseTreeWalker parseTreeWalker = new ParseTreeWalker();
        // 将监听器和语法树传入walk方法，该方法会遍历语法树触发回调
        parseTreeWalker.walk(new ShortToUnicodeString(), tree);
    }
}
